var App = function () {
    var _dataTableOptions = {
        "processing": true,
        "serverSide": true,
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        },
        searching: false,//是否开始本地搜索
        paging: true//是否分页
    };
    var myModal = function () {
        var _init = function (selectTreeCallback) {
            var _tree = "";
            if (selectTreeCallback) {
                _tree = " <ul id=\"tree\" class=\"ztree\"></ul>";
                $(document).on("click","#modal-ok",function(){
                    var treeObj = $.fn.zTree.getZTreeObj("tree");
                    var nodes = treeObj.getSelectedNodes();
                    if(nodes.length==0){
                        _msg("请选择一个类别");
                        return;
                    }
                    selectTreeCallback(nodes[0]);
                    $("#modal-default").modal("hide");
                })
            }
            var _html = '<div class="modal fade" id="modal-default" style="display: none;">\n' +
                '    <div class="modal-dialog">\n' +
                '        <div class="modal-content">\n' +
                '            <div class="modal-header">\n' +
                '                <button type="button" class="close" data-dismiss="modal" aria-label="Close">\n' +
                '                    <span aria-hidden="true">×</span></button>\n' +
                '                <h4 class="modal-title">提示</h4>\n' +
                '            </div>\n' +
                '            <div class="modal-body">\n' +
                '                <p>' + _tree + '</p>\n' +
                '            </div>\n' +
                ' <div class="modal-footer">\n' +
                '                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>\n' +
                '                <button id="modal-ok" type="button"  class="btn btn-primary">确认</button>\n' +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>\n' +
                '</div>';

            $('body').append(_html);
        };
        var _showAlert = function (msg) {
            $(".modal-footer").hide();
            _show(msg)
        };
        var _showTree = function () {
            $("#modal-default").modal();
        }
        var _showConfirm = function (options) {
            var defaultOptions = {tip: "确认要进行该操作么", successMsg: "操作成功"};
            $.extend(defaultOptions, options);
            $(document).off("click", "#modal-ok");
            $(document).on("click", "#modal-ok", function () {
                $("#modal-default").modal("hide");
                $.ajax({
                    url: defaultOptions.url,
                    method: "get",
                    success: function (data) {
                        if (data.success) {
                            App.msg(defaultOptions.successMsg);
                            if (defaultOptions.success) {
                                defaultOptions.success();
                            }

                            _reload();
                        }
                        else {
                            App.msg(data.msg)
                        }
                    }
                });
            });
            $(".modal-footer").show();
            _show(defaultOptions.tip);
        };
        var _show = function (msg) {
            $(".modal-body p").html(msg);
            $("#modal-default").modal();
        };
        return {
            init: _init,
            showTree: _showTree,
            showAlert: _showAlert,
            showConfirm: _showConfirm
        }
    }();

    //validation的自定义手机验证规则
    $.validator.addMethod("mobile", function (value, element) {
        var length = value.length;
        var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "手机号码格式错误");

    var _initFormValidate = function () {
        $("#inputForm").validate({
            errorElement: 'span',
            errorClass: 'help-block',

            errorPlacement: function (error, element) {
                element.parent().parent().attr("class", "form-group has-error");
                error.insertAfter(element);
            },
            highlight: function (element) {
                $(element).parent().parent().attr("class", "form-group has-error");
            },
            success: function (element) {
                element.parent().parent().removeClass("has-error");
            }
        });
    };
    var _dataTable;
    var _initDataTable = function (options) {
        $.extend(_dataTableOptions, options);
        _dataTable = $('#dataTable').DataTable(_dataTableOptions);
        _initSerach();
    };
    var _initSerach = function () {
        $("#search").click(function () {

            var obj = {};

            var array = $("#searchForm").serializeArray();
            for (var i = 0; i < array.length; i++) {
                obj[array[i].name] = array[i].value;
            }

            _dataTable.settings()[0].ajax.data = obj;//{search:$("#searchForm").serializeArray()};
            _reload();
            return false;
        });
    };
    var _reload = function () {
        if(_dataTable){
        _dataTable.ajax.reload();
        }
    };
    var _msg = function (msg) {
        layer.msg(msg);
    };
    return {
        initFormValidate: _initFormValidate,
        initDataTable: _initDataTable,
        myModal: myModal,
        msg: _msg
    }

}();

$(function () {

     App.initFormValidate();

});